<?php
	/**
	 * Core Loader
	 * 
	 * Defines the structure of the CKromeCore class, which bootstraps everything
	 * else in the system.	 	 	 
	 *
	 * @author     Brandon R. Stoner <brandon.stoner@gmail.com>
	 * @copyright  Copyright 2008, Monokromatic Inc.	 
	 * @package    Core
	 * @subpackage Core
	 **/	 		

	/**
	 * Defines the core "bootstrapping" object in Krome
	 * 
	 * Object defining the main application entry of the Krome system. Manages
	 * applications loaded by the system, as well as loads core libraries required
	 * by all Krome applications.
	 * 	 
	 * @author     Brandon R. Stoner <brandon.stoner@gmail.com>
	 * @copyright  Copyright 2008, Monokromatic Inc.	 	 	 	 	 
	 **/
	class CKromeCore
	{
		/**
		 * Array of library objects that the core has instantiated
		 * @var array
		 **/
		private $libraries    = null;

		/**
		 * Array of applications that the core has instantiated
		 * @var array
		 **/
		private $applications = null;

		/**
		 * Calls the required functions to load required library and application
		 * files.
		 *
		 * @param array $libraries
		 * Contains an array of key value pairs. Keys being library names, and
		 * values being boolean values stating whether the library should be
		 * instantiated.
		 *
		 * @param array $applications
		 * Contains an array of key value pairs. Keys being application names,
		 * and values being boolean values stating whether the application should
		 * actually be instantiated.		 		 		 		 
		 **/
		public function __construct( $libraries, $applications )
		{
			$this->libraries    = $this->InitializeLibraries( $libraries    );
			$this->applications = $this->LoadApplications   ( $applications );
		}

		/**
		 * Requires any library files requested, and returns an array of any
		 * objects that were instantiated in the process.
		 * 		 
		 * @param array $libraries
		 * Contains an array of key value pairs. Keys being library names, and
		 * values being boolean values stating whether the library should be
		 * instantiated.
		 *		 
		 * @return array Returns an array of library objects that were created		 
		 **/		 		
		public function InitializeLibraries( $libraries )
		{
			$retVal = Array();

				// Loop through the libraries...
			foreach ( $libraries as $lib=>$autoload )
			{
					// ...and require the specific files needed to use each one.
				$file = realpath( KROME_BASEPATH ) . substr($lib, 6, strlen($lib)-6) . KROME_FILEEXT;
				require( $file );

					// If this needs autoloaded...
				if ( $autoload === true ) {
						// ...create an object, and throw it into the returning array.
	   			$retVal[$lib] = new $lib;
	   		}
			}
		
			return $retVal;
		}

		/**
		 * Takes an array of application names as keys, and instantiates any
		 * applications where the value in the array is set to true.
		 * 		 
		 * @param array $applications
		 * Contains an array of key value pairs. Keys being application names,
		 * and values being boolean values stating whether the application should
		 * actually be instantiated.
		 * 		 
		 * @return array Returns an array of applications that were instantiated
		 **/
		public function LoadApplications( $applications )
		{
			$retVal = Array();
			
				// Loop through our array of applications...
			foreach ( $applications as $app=>$autoload )
			{
					// ...and require any file needed to instantiate a new application.
				require( KROME_APPLICATIONS_DIR . $app . '/' . strtolower($app) . KROME_FILEEXT );

					// If we are supposed to instantiate the object...
				if ( $autoload === true ) {
						// ...create a new application, and throw it into our returning array.
    			$appClassName = 'CKrome' . $app . 'Application';
    			array_push( $retVal, new $appClassName( $this->libraries[ KROME_COMPONENTMANAGER_CLASSNAME ] ) );
    		}
			}
			
			return $retVal;
		}
	}
?>
